package other;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import static java.util.Calendar.DAY_OF_MONTH;

/**
 * description
 *
 * @author 朱宗刚
 * @version 1.0.0
 * @date 2021/11/17
 */


public class EversecDataUtil1 {
    @Test
    public void test01() {
        String path = "E:\\work\\document\\网信办态势感知平台\\样例数据";
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy年MM月dd日");
        Calendar fileCalendar = Calendar.getInstance();
        Calendar dataCalendar = Calendar.getInstance();
        String template1 = "20210831";
        String template2 = "2021-08-31";
        String template3 = "2021年08月31日";
        String template4 = "20210901";
        File targetPath;
        File targetFile;
        File templateFiles = new File("E:\\work\\document\\网信办态势感知平台\\样例数据\\2021-09-01");
        String line;
        fileCalendar.setTimeInMillis(1630425600000L);

        for(int i = 0; i < 90; ++i) {
            fileCalendar.add(DAY_OF_MONTH, 1);
            System.out.println(sdf1.format(fileCalendar.getTime()) + "\t" + sdf2.format(fileCalendar.getTime()) + "\t"+ sdf3.format(fileCalendar.getTime()));
            targetPath = new File(path + File.separator + sdf2.format(fileCalendar.getTime()));
            if(!targetPath.exists()) {
                targetPath.mkdirs();
            }

            dataCalendar.setTimeInMillis(fileCalendar.getTimeInMillis());
            dataCalendar.add(DAY_OF_MONTH, -1);
            for(File file : templateFiles.listFiles()) {
                targetFile = new File(targetPath + File.separator + file.getName().replace(template4, sdf1.format(fileCalendar.getTime())));
                try(BufferedReader fin = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
                    BufferedWriter fout = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetFile)))) {
                    while((line = fin.readLine()) != null) {
                        line = line.replace(template1, sdf1.format(dataCalendar.getTime()));
                        line = line.replace(template2, sdf2.format(dataCalendar.getTime()));
                        line = line.replace(template3, sdf3.format(dataCalendar.getTime()));

                        fout.write(line + "\n");
                    }
                } catch (Exception e) {

                }
            }
        }
    }

    @Test
    public void test02() {
        // dns: 2021-09-24 21:31:12 ~ 2021-09-27 16:09:09 => 1632499200000 ~ 1632671999999
        // http:2021-09-24 03:00:58 ~ 2021-09-25 02:00:23
        // httpLow:2021-09-22 08:04:33 ~ 2021-09-22 07:38:20

        File file = new File("E:\\work\\document\\网信办态势感知平台\\风险事件\\样例数据\\http-detection-result.json");
//        File file2 = new File("E:\\work\\document\\网信办态势感知平台\\风险事件\\样例数据\\httpdetection20210924.json");
        JSONObject data;
        String line;
        Set<String> ips = new HashSet<>();
        try(BufferedReader fin = new BufferedReader(new InputStreamReader(new FileInputStream(file)))/*;
            BufferedWriter fout = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file2)))*/) {
            while((line = fin.readLine()) != null) {
                data = JSONObject.parseObject(line);
                ips.add(data.getString("distIp"));
                ips.add(data.getString("srcIp"));
//                if(data.getLong("requestTimestamp") > 1632499200000L) {
//                    data.replace("requestTimestamp", data.getLongValue("requestTimestamp") - 86400000L);
//                }
//                fout.write(data.toJSONString() + "\n");
            }

            ips.forEach(ip -> System.out.println(ip));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test03() {
        File templateFile = new File("E:\\work\\document\\网信办态势感知平台\\风险事件\\样例数据\\dns-detection-result20210925.json");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
        Calendar fileCalendar = Calendar.getInstance();
        fileCalendar.setTimeInMillis(1632499200000L);
        String targetFileName, line;
        JSONObject data;
        for(int i = -2; i > -27; --i) {
            fileCalendar.add(DAY_OF_MONTH, -2);
            targetFileName = "E:\\work\\document\\网信办态势感知平台\\风险事件\\样例数据\\dns-detection-result" + sdf1.format(fileCalendar.getTime()) + ".json";
            try(BufferedReader fin = new BufferedReader(new InputStreamReader(new FileInputStream(templateFile)));
                BufferedWriter fout = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetFileName)))) {
                while ((line = fin.readLine()) != null) {
                    data = JSONObject.parseObject(line);
                    data.replace("timestamp", data.getLongValue("timestamp") + i * 86400000L);
                    fout.write(data.toJSONString() + "\n");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            --i;
        }
    }

    @Test
    public void test04() {
        File templateFile = new File("E:\\work\\document\\网信办态势感知平台\\风险事件\\样例数据\\httpdetection20210924.json");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
        Calendar fileCalendar = Calendar.getInstance();
        fileCalendar.setTimeInMillis(1632412800000L);
        String targetFileName, line;
        JSONObject data;
        for(int i = 1; i < 75; ++i) {
            fileCalendar.add(DAY_OF_MONTH, 1);
            targetFileName = "E:\\work\\document\\网信办态势感知平台\\风险事件\\样例数据\\httpdetection" + sdf1.format(fileCalendar.getTime()) + ".json";
            try(BufferedReader fin = new BufferedReader(new InputStreamReader(new FileInputStream(templateFile)));
                BufferedWriter fout = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetFileName)))) {
                while ((line = fin.readLine()) != null) {
                    data = JSONObject.parseObject(line);
                    data.replace("requestTimestamp", data.getLongValue("requestTimestamp") + i * 86400000L);
                    fout.write(data.toJSONString() + "\n");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void test05() {
        File templateFile = new File("E:\\work\\document\\网信办态势感知平台\\风险事件\\样例数据\\http-detection-result.json");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
        Calendar fileCalendar = Calendar.getInstance();
        fileCalendar.setTimeInMillis(1632240000000L);
        String targetFileName, line;
        JSONObject data;
        for(int i = 1; i < 75; ++i) {
            fileCalendar.add(DAY_OF_MONTH, 1);
            targetFileName = "E:\\work\\document\\网信办态势感知平台\\风险事件\\样例数据\\http-detection-result" + sdf1.format(fileCalendar.getTime()) + ".json";
            try(BufferedReader fin = new BufferedReader(new InputStreamReader(new FileInputStream(templateFile)));
                BufferedWriter fout = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetFileName)))) {
                while ((line = fin.readLine()) != null) {
                    data = JSONObject.parseObject(line);
                    data.replace("requestTimestamp", data.getLongValue("requestTimestamp") + i * 86400000L);
                    fout.write(data.toJSONString() + "\n");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        fileCalendar.setTimeInMillis(1632240000000L);
        for(int i = -1; i > -25; --i) {
            fileCalendar.add(DAY_OF_MONTH, -1);
            targetFileName = "E:\\work\\document\\网信办态势感知平台\\风险事件\\样例数据\\http-detection-result" + sdf1.format(fileCalendar.getTime()) + ".json";
            try(BufferedReader fin = new BufferedReader(new InputStreamReader(new FileInputStream(templateFile)));
                BufferedWriter fout = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetFileName)))) {
                while ((line = fin.readLine()) != null) {
                    data = JSONObject.parseObject(line);
                    data.replace("requestTimestamp", data.getLongValue("requestTimestamp") + i * 86400000L);
                    fout.write(data.toJSONString() + "\n");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void test06() {
        String path = "E:\\work\\document\\网信办态势感知平台\\样例数据\\大屏数据";
//        String[] types = new String[] {"DDoS攻击", "漏洞利用", "网络风险", "资产信息大屏"};
        String[] types = new String[] {"资产信息大屏"};
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy/MM/dd");
        String templateSuffix = "20210831";
        Calendar fileCalender = Calendar.getInstance();
        fileCalender.setTimeInMillis(1630382134000L);
        String srcFileName;
        String tarFileName;
        String line;
        for(int i = 0; i < 93; ++i) {
            fileCalender.add(DAY_OF_MONTH, 1);
            System.out.println(sdf2.format(fileCalender.getTime()));
            for(String typ : types) {
                srcFileName = path + File.separator + typ + templateSuffix + ".csv";
                tarFileName = path + File.separator + typ + sdf1.format(fileCalender.getTime()) + ".csv";
                try(BufferedReader fin = new BufferedReader(new InputStreamReader(new FileInputStream(srcFileName)));
                    BufferedWriter fout = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tarFileName)))) {
                    while((line = fin.readLine()) != null) {
                        line = line.replace(templateSuffix, sdf1.format(fileCalender.getTime()))
                                .replace("2021-08-31", sdf2.format(fileCalender.getTime()))
                                .replace("2021/08/31", sdf3.format(fileCalender.getTime()));
                        fout.write(line + "\n");
                    }
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    public void test07() {
        List<String> list = Arrays.asList("123", "234", "345", "345", "456", "567", "678", "789", "890");
        System.out.println(list.stream().filter(item -> item.contains("4")).collect(Collectors.joining("-")));
    }
}
